/**
 * weapp-js v1.7.1(2021-4-16)
 * https://codeup.aliyun.com/motigo/motigo/weapp_base
 * Copyright@ 2021 Motigo, Inc.
 * 
 */
Component({
    data: {
        title: '',
        content: '',
        visible: false
    },
    result: null, // showModal结果
    methods: {
        stopEvent() {},
        onHide() {
            this.setData({
                visible: false
            })
        },
        onConfirmModal() {
            this.onHide();
            this.result = {
                confirm: true
            }
        },
        onCancelModal() {
            this.onHide();
            this.result = {
                cancel: true
            }
        },
        showModal(opts) {
            this.result = null;
            this.setData({
                visible: true,
                title: opts.title,
                titleColor: opts.titleColor || '#3e6f77',
                cancelText: opts.cancelText || '取消',
                confirmText: opts.confirmText || '确定',
                showCancel: (opts.showCancel == undefined ? true : opts.showCancel),
                cancelColor: opts.cancelColor || '#FFF',
                confirmColor: opts.confirmColor || '#FFF',
                content: opts.content,
                cancelBgColor: opts.cancelBgColor || '#bfd6d3',
                confirmBgColor: opts.confirmBgColor || '#829c9d'
            });

            (new Promise(resolve => {
                let timer = setInterval(() => {
                    if (this.result) {
                        clearInterval(timer);
                        resolve(this.result);
                    }
                }, 10);
            })).then(res => {
                opts.success && opts.success(res);
                opts.complete && opts.complete(res);
            }, err => {
                opts.fail && opts.fail(err);
            });
        }
    }
})
